import React from 'react'
import { Table, Badge, Modal, Button } from 'antd'
import axios from 'axios'
import { checkresStatus } from 'utils'
import { color, ba } from '../../utils/json'

export default function Log(props) {
  const { current, record } = props
  const {
    chipDetailVo,
    engineDetailVo,
    terminalTypeDetailVo,
    vehicleDetailsVo,
    vehicleTypeDetailsVo,
  } = record
  const [data, setData] = React.useState([])
  const [pageIdx, setPageIdx] = React.useState(1)
  const [pageSize, setPageSize] = React.useState(10)
  const [total, setTotal] = React.useState(0)
  const [loading, setLoading] = React.useState(false)

  const columns = [
    { title: '序号', dataIndex: 'index', render: (v, o, i) => i + 1 },
    {
      title: '类型',
      dataIndex: 'type',
      render: v =>
        [
          '',
          '车型备案',
          '发动机备案',
          '车辆备案',
          '芯片备案',
          '',
          '',
          '',
          '终端备案',
        ][v],
    },
    { title: '申请时间', dataIndex: 'restStartTime' },
    { title: '审批时间', dataIndex: 'restEndTime' },
    {
      title: '状态',
      dataIndex: 'status',
      render: v => <Badge status={color[v]} text={ba[v]} />,
    },
    {
      title: '备注',
      dataIndex: 'errMsg',
      render: v => (
        <Button
          type="link"
          onClick={() => {
            Modal.info({
              title: '备注',
              content: v,
            })
          }}
        >
          查看
        </Button>
      ),
    },
  ]

  React.useEffect(() => {
    setPageIdx(1)
  }, [current])

  React.useEffect(() => {
    let type = null
    let name = null
    switch (current) {
      case 0:
        {
          type = 4
          name = chipDetailVo ? chipDetailVo.chipModel : ''
        }
        break
      case 1:
        {
          type = 8
          name = terminalTypeDetailVo ? terminalTypeDetailVo.name : ''
        }
        break
      case 2:
        {
          type = 2
          name = engineDetailVo ? engineDetailVo.engineModel : ''
        }
        break
      case 3:
        {
          type = 1
          name = vehicleTypeDetailsVo ? vehicleTypeDetailsVo.modelCn : ''
        }
        break
      case 4:
        {
          type = 3
          name = vehicleDetailsVo ? vehicleDetailsVo.vehicleFrameNo : ''
        }
        break
    }
    if (!type || !name) return
    setLoading(true)
    axios
      .get('/restlog/queryRestLog', {
        params: {
          pageIdx: pageIdx,
          pageSize: pageSize,
          type: type,
          recordId: name,
        },
      })
      .then(res => {
        setLoading(false)
        if (!checkresStatus(res)) return
        setData(res.data.data.records)
        setTotal(res.data.data.total)
      })
  }, [current, pageIdx, pageSize, record])

  return (
    <Table
      columns={columns}
      dataSource={data}
      loading={loading}
      rowKey="id"
      pagination={{
        current: pageIdx,
        pageSize: pageSize,
        total: total,
        onChange: (page, pageSize) => {
          setPageIdx(page)
          setPageSize(pageSize)
        },
      }}
    ></Table>
  )
}
